package com.hu.network.task.impl;

import android.content.Context;

import java.io.File;

import com.hu.network.http.bean.BaseRequestBean;
import com.hu.network.http.bean.InnerRequestBean;
import com.hu.network.http.bean.InnerResponseResult;
import com.hu.network.http.request.IHttpRequest;
import com.hu.network.http.request.impl.DownloadHttpRequestImpl;
import com.hu.network.task.AbstractTask;

/**
 * 说明：
 *
 * @author ： hucanhua
 * @date ： 2017/10/28
 */
public class DownloadTaskImpl extends AbstractTask<File> {
    private final BaseRequestBean requestBean;
    private final String filePath;

    public DownloadTaskImpl(Context context, BaseRequestBean requestBean, boolean useHttps, String url, String filePath, DownloadTaskCallback iTaskCallback) {
        super(context,useHttps, url, iTaskCallback);
        this.requestBean = requestBean;
        this.filePath = filePath;
    }

    @Override
    protected boolean before(InnerResponseResult innerResponseResult) {
        return true;
    }

    @Override
    protected void after(InnerResponseResult innerResponseResult) {

    }

    @Override
    protected IHttpRequest<File> getRequest() {
        return new DownloadHttpRequestImpl(new InnerRequestBean<>(useHttps, url, requestBean), filePath, null, new DownloadHttpRequestImpl.ProgressCallback() {
            @Override
            public void progress(long bytesRead, long contentLength, boolean done) {
                if (iTaskCallback != null) {
                    ((DownloadTaskCallback) iTaskCallback).progress(bytesRead, contentLength, done);
                }
            }
        });
    }

    public interface DownloadTaskCallback extends ITaskCallback<File> {
        void progress(long bytesRead, long contentLength, boolean done);
    }
}
